/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package library.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import library.User;

/**
 *
 * @author ryan
 */
public class AddUserPopup extends JFrame{
    public AddUserPopup(final UserPanel p){
        super();
        final JLabel lblName = new JLabel("Name");
        final JTextField name = new JTextField();
        final JComboBox type = new JComboBox();
        for(User.Type t:User.Type.values()){
            type.addItem(t);
        }
        JButton ok = new JButton("ok");
        ok.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!name.getText().isEmpty()){
                    User user = new User(name.getText(), User.Type.valueOf(type.getSelectedItem().toString()));
                    p.addUser(user);
                    AddUserPopup.this.dispose();
                }else{
                    JOptionPane.showMessageDialog(null,"Name cannot be blank","Name error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        JButton cancel = new JButton("cancel");
        cancel.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                AddUserPopup.this.dispose();
            }
        });
        
        GridLayout layout = new GridLayout(0, 3);
        this.getContentPane().setLayout(layout);
        this.add(lblName);
        this.add(name);
        this.add(type);
        this.add(ok);
        this.setLocation(500, 400);
        this.pack();
        this.setVisible(true);
    }
}
    
